<?php
require_once('dl_converter_functions.php');
require_once('dl2co2.php');


//----------------------------
// DIRECTORY INITIALIZATION
//----------------------------
global $BASEDIR,$RAWDATA_DIR, $RAWDATA_FILES_DIR, $DATA_DIR, $CO2_DIR;

$sensorId = 'BMOTE';
$existDirectories = file_exists($BASEDIR);
if ($existDirectories != 1) $existDirectories=0;

if ($existDirectories == 0){
	echo "Creating directory structure \n";
	$path = $BASEDIR;
	mkdir($path, true);

	$path = $RAWDATA_DIR;
	mkdir($path, true);
	
	$path = $RAWDATA_DIR . "/" . $sensorId;
	mkdir($path, true);
	$path = $RAWDATA_DIR . "/" . $sensorId . "/_processed";
	mkdir($path, true);
	
	$path = $DATA_DIR;
	mkdir($path, true);
	$path = $DATA_DIR . "/_processed";
	mkdir($path, true);

    $path = $RAWDATA_FILES_DIR;
	mkdir($path, true);
	$path = $RAWDATA_FILES_DIR . "/_processed";
	mkdir($path, true);

	$path = $CO2_DIR;
	mkdir($path, true);
	$path = $CO2_DIR . "/_processed";
	mkdir($path, true);

}


//----------------------------
// SCRIPT
//----------------------------

convertRawFiles($sensorId);
unifyDuplicates($sensorId);
moveyyyymmddFiles($sensorId);

$t1 = time() - 3600 * 24 * 30 *6; //-6 months
$t2 = time() + 3600*24*30*1; // + 1 month
generateCO2Files($sensorId, $t1, $t2, false);


//----------------------------
// MAIN FUNCTIONS
//----------------------------

function convertRawFiles($sensorId){
	// Parse DL files and split data into yyyymmdd.n files
	// and moves original files to processed folder.
	global $RAWDATA_DIR,$RAWDATA_FILES_DIR;

	//folder where YYYY_MM_DD_n00.txt files are
	$path = $RAWDATA_DIR . "/" . $sensorId;
	$pathFiles = $RAWDATA_FILES_DIR;

  //echo "convertRawFiles $path\n";
	if (is_dir($path)){
		$elements = sort_dir($path);
		$i=0;
		while ($i < sizeof($elements)){
			$element = $elements[$i];
			$name = get_name($element);
			$extension = get_extension($element);
			//echo "convertRawFiles element:$element name:$name extension:$extension\n";
			if($extension == "txt"){
				//echo "txt file - ";
				$filename = $path . "/" . $element;
				$counter = 0;
				//echo "convertRawFiles filename:$filename\n";
				$file = fopen($filename, 'r');
				$first_line = fgets($file);
				$second_line = fgets($file);
				while(!feof($file)){
					$counter++;
					//Obtain the day and month of the first date
					//Create the first file

					// headers
					if ($counter == 6){
						$name = get_file_name($line);
						//if file exists, create the file .A
						$var = 1;
						if (file_exists("$pathFiles/$name.txt")){
							$wfile = fopen("$pathFiles/$name.$var.txt", 'w');
							$var++;
						} else{
							$wfile = fopen("$pathFiles/$name.txt", 'w');
						}
						// writting headers
						fwrite($wfile, $first_line);
						fwrite($wfile, $second_line);
						$day_before = get_day($line);
						$month_before = get_month($line);
					}
					// data
					if ($counter >= 6){
						//Check the date
						$day = get_day($line);
						$month = get_month($line);

						//If same day, add line
						if(($month == $month_before)&&($day == $day_before)){
							fwrite($wfile, $line);
						}//If different day, create file
						else {//get file name
							$var2 = 1;
							$name = get_file_name($line);
							if (file_exists("$pathFiles/$name.txt")){
								if(($month > $month_before)||($day > $day_before)){
									fclose($wfile);
									$year = get_year($line);
									$wfile = fopen("$pathFiles/$year"."$month"."$day.$var.txt", "a+");
									$var++;
									fwrite($wfile, $first_line);
									fwrite($wfile, $second_line);
									fwrite($wfile, $line);
									$month_before = $month;
									$day_before = $day;
								}
							} else if (!file_exists("$pathFiles/$name.txt")){
								if(($month > $month_before)||($day > $day_before)){
									fclose($wfile);
									$year = get_year($line);
									$wfile = fopen("$pathFiles/$year"."$month"."$day.txt", "a+");
									fwrite($wfile, $first_line);
									fwrite($wfile, $second_line);
									fwrite($wfile, $line);
									$month_before = $month;
									$day_before = $day;
								}
							}
						}

					}
					//$file -> next();
					$var = 1;
					//$line = $file -> current();
					$line = fgets($file);
				}
				fclose($file);
				// move DL file to processed folder
				$folder = getProccesedFolder($path); //$path = $RAWDATA_DIR . "/" . $sensorId
				$source = $path . "/" . $element;
				$target = $folder . "/" . $element;
				move_processed($source, $target);
			}
			$i++;
		}
	}
}

function moveyyyymmddFiles($sensorId){
	global $RAWDATA_DIR,$RAWDATA_FILES_DIR, $DATA_DIR;
	$path = $RAWDATA_FILES_DIR;

	if (is_dir($path)){
		$dir = opendir($path);
		$elements = sort_txt_dir($dir);
		foreach ( $elements as $element) {
			// clean processed files
			$processedDir = getProccesedFolder($path);
			$source = "$path/$element";
			$target = "$DATA_DIR/$element";
			// copy to data directory
			copy($source, $target);
			// move to processed files
			$target = "$processedDir/$element";
			move_processed($source, $target);
		}
	}
}

function unifyDuplicates($sensorId){
	// From yyyymmdd.n files generates a unique file yyyymmdd
	global $RAWDATA_DIR,$RAWDATA_FILES_DIR, $DATA_DIR;
	$path = $RAWDATA_FILES_DIR;
	$dir = opendir($path);
	$elements = sort_txt_dir($dir);
	$i=0;
//print_r($elements);	
	while ($i < sizeof($elements)) {
		$filename = $elements[$i]; // yyyymmdd.txt or yyyymmdd.N.txt
		$id = substr($filename, 0, 8);
		$files = getSameDateFiles($id, $elements);
		if (sizeof($files)>1){
//echo "$id\n";
//print_r($files);			
			//unify files
			$allFilename = "$path/$id.all";
			unifyFiles($files, $allFilename);
			// clean processed files
			foreach ( $files as $file) {
				// clean processed files
				$processedDir = getProccesedFolder($path);
				$source = "$path/$file";
				$target = "$processedDir/$file";
//echo "Move $source to $target\n";				
				move_processed($source, $target);
			}
			// rename unified
			$target = "$path/$id.txt";
//echo "RENAME $allFilename to $target\n";
			rename($allFilename, $target);				
			$i = $i + sizeof($files);
		}
		else $i++;
		
	}
}

function unifyFiles($files, $targetFilename){
	//concatenates the content of the files into a new file
	global $RAWDATA_DIR,$RAWDATA_FILES_DIR, $DATA_DIR;
	$path = $RAWDATA_FILES_DIR;
	$targetFile = fopen($targetFilename, "w");
	$counter = 0;
	
	//FIXME: inverse ordering to maintain time ordering
	rsort($files);
	
	foreach ($files as $filename){
		$file = new SplFileObject("$path/$filename");
  		foreach ($file as $k => $line) {
  			$lineNumber = $file->key() + 1;
  			$line =$file->current();
  			$includeLine = true;
  			if ($counter>0 & $lineNumber < 3) $includeLine = false;
			if ($includeLine) fwrite($targetFile, $line);
  		}
  		$counter++;
	}
	fclose($targetFile);
}

function getSameDateFiles($pattern, $files){
	// returns an array with the same filename pattern
	// yyyymmdd.txt, yyyymmdd.1.txt, yyyymmdd.2.txt
	// always returns the $fileNamePattern file
	$i =0;
//echo "pattern $pattern\n";
	foreach ( $files as $file) {
//		$name = substr($file, 0,strlen($pattern)); 
//echo "$name\n$pattern\n";		
		if (substr_compare( $file, $pattern , 0 , strlen($pattern) ) == 0) {
			$sameFiles[$i] = $file;
			$i++;
		}
	}
	return $sameFiles;
	
	
}

function getProccesedFolder($folder){ //$path = $RAWDATA_DIR . "/" . $sensorId
	return $folder .  "/_processed";
}



?>